package javawdw.util.dlsoft.crypt;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javawdw.util.crypt.Base64;

/**
 * HTTP或FTP下载链接
 * 
 * @author David
 * 
 */
public final class TPLink {
	private String url;

	public String getUrl() {
		return this.url;
	}

	TPLink(String url) {
		this.url = url;
	}

	/**
	 * 将当前下载链接转化成迅雷链接
	 * 
	 * @return 转化后的下载链接
	 */
	public SoftLink toThunder() {
		return this.toOther(LinkType.THUNDER);
	}

	/**
	 * 将当前下载链接转化成快车链接
	 * 
	 * @return 转化后的下载链接
	 */
	public SoftLink toFlashget() {
		return this.toOther(LinkType.FLASHGET);
	}

	/**
	 * 将当前下载链接转化成QQ链接
	 * 
	 * @return 转化后的下载链接
	 */
	public SoftLink toQqdl() {
		return this.toOther(LinkType.QQDL);
	}

	private SoftLink toOther(LinkType linkType) {
		String disguiseLink = linkType.getStart() + this.url
				+ linkType.getEnd();
		String base64Link = Base64.encode(disguiseLink);
		String resultLink = linkType.getHead() + base64Link
				+ linkType.getEncodeEndExtra();
		Pattern p = Pattern.compile("\\s*|\t|\r|\n");
		Matcher m = p.matcher(resultLink);
		resultLink = m.replaceAll("");
		return new SoftLink(resultLink);
	}
}